<template>
	<view>
		<view class="goods-list">
			<view v-for="(goods,i) in goodsList" :key="i" @click="gotoGoodsDetail(goods)">
				<my-goods :goods="goods"></my-goods>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				queryObj: {
					cid: '',
					query: '',
					pageSize: 10,
					pageNum: 1
				},
				goodsList: [],
				total: 0,
				isloading: false
			};
		},
		onLoad(options) {
			this.queryObj.cid = options.cid || ''
			this.queryObj.query = options.query || ''
			this.getGoodsList()
		},
		methods: {
			async getGoodsList() {
				this.isloading = true
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
				if (res.meta.status !== 200) return uni.$showMsg()
				this.goodsList = [...this.goodsList, ...res.message.goods]
				this.total = res.message.total
				this.isloading = false
			},
			gotoGoodsDetail(goods){
				uni.navigateTo({
					url: '/subpkg/goods-detail/goods-detail?goods_id=' + goods.goods_id
				})
			}
		},
		onReachBottom() {
			if(this.queryObj.pageNum * this.queryObj.pageSize >= this.total) return uni.$showMsg('数据加载完毕!')
			if(this.isloading) return
			this.queryObj.pageNum += 1
			this.getGoodsList()
		}
	}
</script>

<style lang="scss">

</style>